
import type { FilterRuleOptions, TypeConfig, RuleGenerationResult } from './types';
import { createBaseRule } from './ruleGeneratorUtils';

const equipmentConfig: TypeConfig = {
    class: 'Maps',
    icon: 'White',
    iconShape: 'Square',
    name: '地图',
    fontSize: 45
};

export function generateEquipmentRule(options: FilterRuleOptions): RuleGenerationResult {
    let baseResult = createBaseRule(options, {
        name: equipmentConfig.name,
        class: equipmentConfig.class,
        defaultColor: '#FFFFFF',
        defaultBgColor: '#000000',
        fontSize: equipmentConfig.fontSize,
        icon: equipmentConfig.icon,
        iconShape: equipmentConfig.iconShape
    });

    // 如果规则无效或被禁用，直接返回
    if (!baseResult.isValid || !baseResult.content) {
        return baseResult;
    }

    // 添加地图特有的条件
    const lines = baseResult.content.split('\n');
    const insertIndex = lines.findIndex(line => line.includes('BaseType')) + 1;

    // 从选项值中提取地图等级
    const tierMatch = options.value.match(/Tier (\d+)/);
    if (tierMatch) {
        const tier = parseInt(tierMatch[1]);

        // 根据地图等级设置不同的颜色和图标
        if (tier >= 14) {  // 高级红图
            lines.push('  SetTextColor 255 0 0');
            lines.push('  SetBorderColor 255 0 0');
            lines.push('  MinimapIcon 0 Red Square');
            lines.push('  PlayEffect Red');
        } else if (tier >= 11) {  // 红图
            lines.push('  SetTextColor 255 100 100');
            lines.push('  SetBorderColor 255 100 100');
            lines.push('  MinimapIcon 1 Red Square');
        } else if (tier >= 6) {  // 黄图
            lines.push('  SetTextColor 255 255 0');
            lines.push('  SetBorderColor 255 255 0');
            lines.push('  MinimapIcon 1 Yellow Square');
        } else {  // 白图
            lines.push('  SetTextColor 255 255 255');
            lines.push('  SetBorderColor 255 255 255');
            lines.push('  MinimapIcon 2 White Square');
        }

        // 添加地图等级条件
        lines.splice(insertIndex, 0, `  MapTier = ${tier}`);
    }

    // 添加特殊音效
    if (options.displayEnabled && options.soundEnabled) {
        lines.push('  PlayAlertSound 4 200');
    }

    baseResult.content = lines.join('\n');
    return baseResult;
}